package com.frx.sort.myqueue;

import java.util.Arrays;

/**
 * Created with IDEA
 *
 * @author: xrf
 * @Date: 2019/10/25
 * @Time: 15:36
 */
public class ArrLookQueue {

    int[] arr;
    int capacity;
    int head = 0;
    int tail = 0;

    public ArrLookQueue(int capacity){
        arr = new int[capacity];
        this.capacity = capacity;
    }
    public static void main(String[] args){
        ArrLookQueue lookQueue = new ArrLookQueue(5);
        lookQueue.enqueue(1);
        lookQueue.enqueue(2);
        lookQueue.enqueue(3);
        lookQueue.enqueue(4);
        System.out.println(Arrays.toString(lookQueue.arr));
        System.out.println(lookQueue.dequeue());
        lookQueue.enqueue(5);
        System.out.println(Arrays.toString(lookQueue.arr));
        System.out.println(lookQueue.dequeue());
        lookQueue.enqueue(6);
        System.out.println(Arrays.toString(lookQueue.arr));
        System.out.println("now head is " + lookQueue.head + " tail is " + lookQueue.tail);
    }




    public boolean enqueue(int a){
        if ((tail + 1) % capacity == head){
            throw new IllegalStateException("queue is full !");
        }
        arr[tail] = a;
        tail = (tail + 1) % capacity;
        return true;
    }
    public int dequeue(){
        if (tail == head){
            throw new IllegalStateException("");
        }
        int ret = arr[head];
        head = (head + 1) % capacity;
        return ret;
    }

}
